package com.jit.monitor.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.jit.baselib.ui.base.BaseActivity;
import com.jit.baselib.ui.base.BasePresenter;
import com.jit.baselib.util.ui.UIUtils;
import com.jit.monitor.R;
import com.jit.monitor.model.CheckDataManager;
import com.jit.monitor.model.bean.AlarmRangeBean;
import com.jit.monitor.model.cache.UserCache;
import com.jit.monitor.model.request.WarnRequest;
import com.jit.monitor.presenter.WarnAtPresenter;
import com.jit.monitor.ui.adapter.AlarmRangAdapter;
import com.jit.monitor.view.WarnAreaAtView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author crazyZhangxl on 2019-1-21 13:22:37.
 * Describe:
 */

public class WarnAreaActivity extends BaseActivity<WarnAreaAtView,WarnAtPresenter> implements WarnAreaAtView, AlarmRangAdapter.CheckBoxChangedListener {

    @BindView(R.id.ivToolbarNavigation)
    ImageView mIvToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View mVToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView mTvToolbarTitle;
    @BindView(R.id.llToolbarTitle)
    LinearLayout mLlToolbarTitle;
    @BindView(R.id.tvEnsure)
    TextView mTvEnsure;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    private List<String> mMSelectedList = new ArrayList<>();
    private List<AlarmRangeBean> mAlarmRangeBeanList = new ArrayList<>();
    private AlarmRangAdapter mAlarmRangAdapter;

    @Override
    protected void init() {
        // 进行数据的加载操作
        HashMap<String, String> textEntry = CheckDataManager.getTextEntry();
        Iterator<Map.Entry<String, String>> iterator = textEntry.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> next = iterator.next();
            mAlarmRangeBeanList.add(new AlarmRangeBean(next.getValue()));
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_warn_area;
    }

    @Override
    protected WarnAtPresenter createPresenter() {
        return new WarnAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTvToolbarTitle.setText("设置预警范围");
        initAdapter();
    }

    private void initAdapter() {
        mRecyclerView.setLayoutManager(new GridLayoutManager(this,3));
        mAlarmRangAdapter = new AlarmRangAdapter(R.layout.item_area, mAlarmRangeBeanList, this);
        mRecyclerView.setAdapter(mAlarmRangAdapter);
    }

    @Override
    protected void initData() {
        mPresenter.queryRange();
    }

    @Override
    protected void initListener() {
        mIvToolbarNavigation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        mTvEnsure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                defWhoIsChecked();
            }
        });
    }

    /**
     * 判断已经选中的名称
     */
    private void defWhoIsChecked(){
        mMSelectedList.clear();
        for (AlarmRangeBean alarmRangeBean:mAlarmRangeBeanList){
            if (alarmRangeBean.isState()){
                mMSelectedList.add(alarmRangeBean.getName());
            }
        }

        if (mMSelectedList.size() == 0){
            showMaterialDialog("预警范围确认", "暂时未设置预警范围,是否提交?", "提交", "再看看",
                    new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            dialog.dismiss();
                            mPresenter.setAlarmRange(mMSelectedList);
                        }
                    }, new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            dialog.dismiss();
                        }
                    });
        }else {
            StringBuilder sb = new StringBuilder();
            for (String str:mMSelectedList){
                sb.append(str).append("/");
            }
            showMaterialDialog("预警范围确认", "设置预警范围为"+sb.substring(0,sb.length()-1)+",是否提交?", "提交", "再看看",
                    new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            dialog.dismiss();
                            mPresenter.setAlarmRange(mMSelectedList);
                        }
                    }, new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            dialog.dismiss();
                        }
                    });
        }
    }

    @Override
    public void queryRangSuccess(List<String> list) {
        if (list != null && list.size() != 0){
            for (int i=0;i<list.size();i++){
                String returnStr = list.get(i);
                for (int j=0;j<mAlarmRangeBeanList.size();j++){
                    if (returnStr.equals(mAlarmRangeBeanList.get(j).getName())){
                        mAlarmRangeBeanList.get(j).setState(true);
                    }
                }
            }
        }
        mAlarmRangAdapter.notifyDataSetChanged();
    }

    @Override
    public void queryRangFailure(String error) {
        UIUtils.showToast(error);
    }

    @Override
    public void setRangFailure(String msg) {
        UIUtils.showToast(msg);
    }

    @Override
    public void setRangSuccess(List<String> mList) {
        if (mList.size() == 0){
            UIUtils.showToast("暂为设置任何预警范围");
        }else {
            StringBuilder sb = new StringBuilder();
            for (String str:mList){
                sb.append(str).append("/");
            }
            UIUtils.showToast("成功设置预警范围:"+sb.substring(0,sb.length()-1));
        }
        finish();
    }

    @Override
    public void onCheckBoxChanged(int position, boolean state) {
        mAlarmRangeBeanList.get(position).setState(state);
    }
}
